﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.VisualBasic.ApplicationServices;

namespace 单实例应用程序
{

    class WpfApp : System.Windows.Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        public void ShowWindow()
        {
            //MainWindow win = new MainWindow();
            //win.Show();

        }
    }
    
    public class SingleInstanceAppliccationWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceAppliccationWrapper()
        {
            this.IsSingleInstance = true;
        }

        private WpfApp app;
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            return base.OnStartup(eventArgs);
            app = new WpfApp();
            app.Run();
            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            app.ShowWindow();
        }
    }
    public class Startup
    {
        [STAThread()]
        public static void Main(string[] args)
        {
            SingleInstanceAppliccationWrapper wrapper = new SingleInstanceAppliccationWrapper();
            wrapper.Run(args);
        }
    }
}
